package org.ade.monak.anak.boundary;

import java.util.ArrayList;
import java.util.List;

import org.ade.monak.anak.Variable.Operation;
import org.ade.monak.anak.entity.Anak;
import org.ade.monak.anak.entity.DataMonitoring;
import org.ade.monak.anak.service.storage.DatabaseManagerAnak;
import org.ade.monak.anak.util.BundleEntityMaker;
import org.ade.monak.anak.util.EntityBundleMaker;
import org.ade.monak.anak.util.IFormOperation;
import org.ade.monak.anak.R;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DaftarMonitoring extends ListActivity implements IFormOperation{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_monak);
		Intent intent = getIntent();
		final Bundle bundle = intent.getExtras();
		Anak anak = EntityBundleMaker.getAnakFromBundle(bundle);
		databaseManager = new DatabaseManagerAnak(this);
		if(anak==null){
			dataMonitorings	= databaseManager.getAllDataMonitorings(true,true);
			justView = true;
		}
		if(dataMonitorings==null){
			dataMonitorings = new ArrayList<DataMonitoring>();
		}
		daftarMonitoringAdapter	= 
				new AdapterDaftarMonitoring(this, R.layout.daftar_monitoring_item, dataMonitorings);
		
		ImageView ivAdd = (ImageView) findViewById(R.id.listMonakIvAdd);
		ivAdd.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				onAdd(bundle);
			}
		});
		
		ImageView ivMap = (ImageView) findViewById(R.id.listMonakMap);
		
		
		if(justView){
			ivAdd.setVisibility(View.GONE);
			ivMap.setVisibility(View.GONE);
		}
		
		getListView().setAdapter(daftarMonitoringAdapter);
		
	}

	public void onAdd(Bundle bundle) {
	
	}

	public void onEdit(Bundle bundle, String idOrtu) {
		Intent intent = new Intent(this, PendaftaranMonitoring.class);
		bundle.putBoolean(PendaftaranMonitoring.EXTRA_EDIT, true);
		bundle.putString("idOrtu", idOrtu);
		if(justView){
			bundle.putBoolean(PendaftaranMonitoring.EXTRA_JUST_VIEW, true);
		}
		intent.putExtras(bundle);
		startActivityForResult(intent, Operation.EDIT);
	}

	public void onDelete(Bundle bundle) {
		showDialog(Operation.DELETE, bundle);
	}
	
	public void deleteSave(Bundle bundle){
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
	}

	@Override
	protected Dialog onCreateDialog(int id, final Bundle bundle) {
		switch(id){
			case Operation.MULTIPLE_CHOICE:{
				return null;
			}case Operation.DELETE:{
				  

				
			}
		}
		return super.onCreateDialog(id, bundle);
	}
		
	private DatabaseManagerAnak 		databaseManager;
	private AdapterDaftarMonitoring daftarMonitoringAdapter;
	private List<DataMonitoring>	dataMonitorings;
	
	private boolean justView = false;
	
	private final static class AdapterDaftarMonitoring extends ArrayAdapter<DataMonitoring>{

		public AdapterDaftarMonitoring(DaftarMonitoring daftarMonitoring,
				int textViewResourceId, List<DataMonitoring> objects) {
			super(daftarMonitoring, textViewResourceId, objects);
			
			this.daftarMonitoring	= daftarMonitoring;
			this.resource 			= textViewResourceId;
			this.dataMonitorings	= objects;
		}
		

		@Override
		public int getCount() {
			return dataMonitorings!=null?dataMonitorings.size():0;
		}


		@Override
		public DataMonitoring getItem(int position) {
			return super.getItem(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			
			if(dataMonitorings!=null&&dataMonitorings.size()>0){
				
			
				DataMonitoring dataMonitoring = dataMonitorings.get(position);
				
				if(dataMonitoring == null) return null;
				
				LayoutInflater inflater = (LayoutInflater) daftarMonitoring
						
				        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				
				View rowView = inflater.inflate(resource, parent, false);
				
				TextView keterangan	= (TextView) rowView.findViewById(R.id.daftarMonitoringText);
				keterangan.setText(dataMonitoring.getKeterangan());
				
				rowView.setOnClickListener(new DaftarMonitoringClick(daftarMonitoring, dataMonitoring));
				
				LinearLayout llBackground = (LinearLayout) rowView.findViewById(R.id.background);
				if(dataMonitoring.isSeharusnya()){

					llBackground.setBackgroundResource(R.drawable.back_menu_green);
				}else{

					llBackground.setBackgroundResource(R.drawable.back_menu);
				}
				
				
				return rowView;
			}else{
				return null;
			}
		}
		
		private final DaftarMonitoring daftarMonitoring;
		private final int resource;
		private List<DataMonitoring> dataMonitorings;
		
	}
	
	private final static class DaftarMonitoringClick implements View.OnClickListener{

		public DaftarMonitoringClick(DaftarMonitoring daftarMonitoring, DataMonitoring dataMonitoring){
			mDaftarMonitoring 	= daftarMonitoring;
			mDataMonitoring 	= dataMonitoring;
		}
		public void onClick(View v) {
			mDaftarMonitoring.onEdit(BundleEntityMaker.makeBundleFromDataMonitoring(mDataMonitoring), mDataMonitoring.getOrtu().getIdOrtu());
		}
		

		private final DataMonitoring mDataMonitoring;
		private final DaftarMonitoring mDaftarMonitoring;
		
	}
	

	public void onSave(Bundle bundle, int status) {
		
	}

	public void onEdit(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	

}
